package cn.gs.global.server.meta;

// @author gs
import cn.gs.db.HibernateUtil;
import cn.gs.db.BoolTransaction;
import cn.gs.global.common.timer.ITimerListener;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TimerListenerImpl implements ITimerListener {

    HibernateUtil util;
    int date;

    public TimerListenerImpl(HibernateUtil util) {
        this.util = util;
    }

    @Override
    public void onNewYear(int year) {
    }

    @Override
    public void onNewMonth(final int month) {
        //backup snapshot
        Logger.getLogger(TimerListenerImpl.class.getName()).log(Level.INFO, "New_Month:{0}", month);
        util.transaction(new BoolTransaction() {

            @Override
            public boolean work() {
                util.executeSQL("INSERT INTO ognkeepbackup(ogn_id, obj_id, cnt, exi, month) SELECT ogn_id, obj_id, cnt, exi, " + month + " as month FROM keep");
                return true;
            }
        });
    }

    @Override
    public void onNewDay(int date) {
        this.date = date;
        //obj flow
        Logger.getLogger(TimerListenerImpl.class.getName()).log(Level.INFO, "New_Day:{0}", date);
        util.transaction(new BoolTransaction() {

            @Override
            public boolean work() {
                util.executeSQL("UPDATE keep SET exi=exi+cnt, cnt=cnt+c1*100, c1=c1+c2, c2=c2+c3, c3=c3+c4, c4=c4+c5");
                return true;
            }
        });
    }

    @Override
    public void onHeartbeat(int date) {
    }
}
